package com.pet.taglib;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;
import org.apache.struts2.views.jsp.StrutsBodyTagSupport;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;

import com.pet.framework.constant.ATTFrameWork;
import com.pet.framework.constant.TreeNode;
import com.pet.framework.util.ConstantUtil;
import com.pet.framework.util.SpringUtil;
import com.pet.framework.util.StringUtil;
import com.pet.role.util.Logger;
import com.pet.server.bo.role.RoleService;
import com.pet.server.po.Constant;

public class ConstantsTag extends StrutsBodyTagSupport {
	private static final Logger logger = Logger.getLogger(ConstantsTag.class);
    private Object value;
    private String codename;
    private String code;
    private String type;
    private RoleService roleService;
    

	@Override
	public int doEndTag() throws JspException {
		return EVAL_PAGE;
	}
	@Override
	public int doStartTag() throws JspException {
		    StringBuffer sBuf = new StringBuffer();
		    ConstantUtil util = ConstantUtil.getInstance();
		    
		    
		    TreeNode<Constant> node = null;
		    if(!StringUtil.isEmpty(codename)){
		    	String pcode = StringUtil.getProperties(codename);
		    	if(!StringUtil.isEmpty(pcode)){
		    		node = util.findNode(pcode);//roleService.queryConstantsByPcode(pcode,ATTFrameWork.LOGIC_TRUE);
		    	}
		    }else if(!StringUtil.isEmpty(code)){
		    	node  = util.findNode(code);//roleService.queryConstantsByPcode(code,ATTFrameWork.LOGIC_TRUE);
		    }else{
		    	//list = new ArrayList<Constant>();
		    }
		    try {
				pageContext.getOut().write(createOptions(node,sBuf,null));
			} catch (IOException e) {
				logger.logError("constants tag error:"+e.getMessage());
			}
		
		
		return EVAL_BODY_BUFFERED;
	}
	
	
	
	
	public String createOptions( TreeNode<Constant> node,StringBuffer sBuf,String name){
		if(null!=node){
			for(TreeNode<Constant> nd:node.nodelist){
				sBuf.append("<option value=\""+nd.t.getCode()+"\"");
				if(value!=null){
				if(StringUtil.equals(nd.t.getCode(),value.toString())){
					sBuf.append("selected=\"selected\"");
				}
				}
				sBuf.append(">");

				int len = nd.t.getCode().length()/3;
				if(len>=3){
					sBuf.append("|");
					for(int i=0;i<len-2;i++){
						sBuf.append("----");	
					}
	                			
				}
				sBuf.append(nd.t.getContsName());
				sBuf.append("</option>");
				if("tree".equals(type)){
					 //List<Constant>  l  = roleService.queryConstantsByPcode(conts.getCode(),ATTFrameWork.LOGIC_TRUE);
					 createOptions(nd, sBuf,nd.t.getContsName());
				}
			}

		}
		return sBuf.toString();
	}


	public String getCodename() {
		return codename;
	}
	public void setCodename(String codename) {
		this.codename = codename;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public Object getValue() {
		return value;
	}
	
	public void setValue(Object value){
			try {
				if(value!=null){
				this.value = ExpressionEvaluatorManager.evaluate(
				        "value", value.toString(), Object.class, this, pageContext);
				}
			} catch (JspException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
	
	
	
}
